/**
 * @Title: uploader.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author QIANDUO875
 * @date 2014年8月19日 下午3:48:10
 * @version V1.0
 */
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.directives');
	app.directive('uploadInput', ['BaseURL',function(baseUrl){
		
		function ctrler($scope, $element, $attrs, $transclude) {
			var ctrl = this;
			$scope.accepts = $attrs.accepts || ['txt','jpg'];
			var files = $scope.$eval($attrs.files) || [];
			$scope._Files = files;
			
			
			$scope.$watchCollection($attrs.files,function(v){
				files = v || [];
				$scope._Files = v || [];
			});
			
			
			ctrl.delFile = function(file) {
				if(file == null) {
					return;
				}
				for(var i=0;i<files.length;i++) {
					if(files[i].name == file.name) {
						files.splice(i,1);
						break;
					}
				}
			}
			
			function findFile(data) {
				for (var i = 0; i < files.length; i++) {
					var file = files[i];
					if (file.name == data.name) {
						return i;
					}
				}
				return -1;
			}
			$scope.onFileSelect = function(fs) {
//				files.splice.apply(files,[0,files.length].concat(fs));
				angular.forEach(fs, function(v,k) {
					if (findFile(v) == -1) {
						files.push(v);
					}
				});
			}
			
		}
		
		return {
			restrict: 'E',
			scope:true,
			replace:true,
            templateUrl:baseUrl+'views/hcpms/common/directives/uploadInput_view.html',
			controller:ctrler
		};
		
	}]).
	
	
	directive('uploadInputFile',[function(){
		return {
			restrict: 'A',
			scope:true,
			require:'^uploadInput',
			replace:false,
            link:function(scope, ele, attrs, ctrl){
            	var files =[];
            	scope.close = function() {
            		ctrl.delFile(scope.file);
            	}
            	
            	
            }
		};
		
	}]);
	
})();